﻿using System;

namespace Borg
{
    [Serializable]
    public class Extension : MetaElement
    {
        private ExtensionType _extensionType;
        private MetaElementCollection<Plug> _plugs;
        private MetaElementCollection<Slot> _slots;

        internal object extensionObject;

        public ExtensionType ExtensionType
        {
            get { return _extensionType; }
            private set { _extensionType = value; }
        }

        public MetaElementCollection<Plug> Plugs
        {
            get { return _plugs; }
            internal set { _plugs = value; }
        }

        public MetaElementCollection<Slot> Slots
        {
            get { return _slots; }
            internal set { _slots = value; }
        }

        public object Object
        {
            get { return Composer.GetExtensionObject(this); }
        }

        public bool IsActivated
        {
            get { return InstanceStore.IsActivated(this); }
        }

        public bool IsShared
        {
            get { return InstanceStore.GetShared(this); }
        }

        public event EventHandler<ExtensionEventArgs> Activated
        {
            add { Notifier.AddActivatedHandler(this, value); }
            remove { Notifier.RemoveActivatedHandler(this, value); }
        }

        public event EventHandler<ExtensionEventArgs> Deactivated
        {
            add { Notifier.AddDeactivatedHandler(this, value); }
            remove { Notifier.RemoveDeactivatedHandler(this, value); }
        }

        internal Extension(int id, ExtensionType extensionType)
            : base(id, extensionType.Name)
        {
            ExtensionType = extensionType;
        }

        public object Activate()
        {
            return Composer.Activate(this);
        }

        public void Deactivate()
        {
            Composer.Deactivate(this);
        }

        public void Destroy()
        {
            Composer.Destroy(this);
        }

        internal override AssemblyElement GetAssemblyElement()
        {
            return ExtensionType.Plugin;
        }
    }
}
